package com.tramp.controller;

import com.tramp.basic.entity.Pickering;
import com.tramp.basic.entity.Product;
import com.tramp.basic.entity.Vpcprice;
import com.tramp.basic.service.PickeringService;
import com.tramp.basic.service.VpcpriceService;
import com.tramp.frame.server.base.BaseController;
import com.tramp.frame.server.base.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理人员控制层
 *
 * @author Shun J
 * @since 2017-11-04 17:29:32
 */
@Controller
@RequestMapping("/product/")
public class ProductController extends BaseController {
    private static final int DEFAULT_QUANTITY = 1;
    private static final String COMPANY_PICKERING = "pickering";
    private static final String COMPANY_VPC = "vpc";
    @Autowired
    private PickeringService pickeringService;
    @Autowired
    private VpcpriceService vpcpriceService;

    /**
     * 列表页
     *
     * @return list返回的界面url
     */
    @RequestMapping(value = "list")
    public String list() {
        return "product/list";
    }

    /**
     * 列表数据
     *
     * @param company  厂商名
     * @param pn       产品编号
     * @param quantity 数量
     * @return 产品列表
     */
    @RequestMapping("/query")
    @ResponseBody
    public ResultData query(String company, String pn, String quantity) {
        List<Product> products = new ArrayList<>();
        if (null == pn || pn.isEmpty()) {
            return success(products);
        }
        if (COMPANY_PICKERING.equals(company)) {
            this.setPageSqlId("PickeringBaseDao.listByEntity");
            Pickering pickering = new Pickering();
            pickering.setPn(pn);
            convertPickeringToProduct(products, pickering);
        }
        if (COMPANY_VPC.equals(company)) {
            this.setPageSqlId("VpcpriceBaseDao.listByEntity");
            Vpcprice vpcprice = new Vpcprice();
            vpcprice.setPn(pn);
            if (null != quantity && !quantity.isEmpty()){
                vpcprice.setQuant(Integer.parseInt(quantity));
            } else {
                vpcprice.setQuant(DEFAULT_QUANTITY);
            }
            convertVpcToProduct(products, vpcprice);
        }
        return successPage(products);
    }

    private void convertVpcToProduct(List<Product> products, Vpcprice vpcprice) {
        List<Vpcprice> vpcpriceList = vpcpriceService.listByEntity(vpcprice);
        if (!vpcpriceList.isEmpty()) {
            for (Vpcprice vpcprice1 : vpcpriceList) {
                Product product = new Product();
                product.setId(vpcprice1.getId());
                product.setPn(vpcprice1.getPn());
                product.setCompany(COMPANY_VPC);
                product.setDescription(vpcprice1.getDescription());
                product.setChinaPrice(vpcprice1.getChinaPrice());
                product.setUsPrice(vpcprice1.getUsPrice());
                product.setDeliveryTime(vpcprice1.getDeliveryTime());
                product.setNotes(vpcprice1.getNotes());
                products.add(product);
            }
        }
    }

    private void convertPickeringToProduct(List<Product> products, Pickering pickering) {
        List<Pickering> pickerings = pickeringService.listByEntity(pickering);
        if (!pickerings.isEmpty()) {
            for (Pickering pickeringProduct : pickerings) {
                Product product = new Product();
                product.setPn(pickeringProduct.getPn());
                product.setCompany(COMPANY_PICKERING);
                product.setId(pickeringProduct.getId());
                product.setChinaPrice(pickeringProduct.getChinaPrice());
                product.setUsPrice(pickeringProduct.getUsPrice());
                product.setDescription(pickeringProduct.getDescription());
                products.add(product);
            }
        }
    }


}
